<?php
abstract class AbstractFieldsController extends EavAppController {

	var $helpers = array('Html', 'Form','Cms.Index');
	var $fieldModelAlias = null;
	var $displayName;
	var $abstractView = "eav.fields";
	function beforeFilter()
	{
		$this->fieldModelAlias = $this->modelNames[0];
		$this->displayName = $this->fieldModelAlias;
		$this->paginate['order'] = $this->fieldModelAlias.'.order ASC';
		parent::beforeFilter();
	}
	function admin_add() 
	{
		checkSessionKey();
		$this->pageTitle = __($this->displayName);
		if($this->_getFieldModel()->useFieldset() == true && $this->_getFieldModel()->getFieldsetModel()->hasAny() == false)
		{
			$this->warningMsg(sprintf(__("Please create a fieldset before adding %s"),$this->displayName));
			$this->redirect(array('controller'=>"fieldsets",'action'=>"add",'plugin'=>"eav"));
		}
		
		if($this->_getFieldModel()->useFieldset() == true)
		{	
			
			$fieldsetList = $this->_getFieldModel()->getFieldsets();
			$fieldsetModelAlias = $this->_getFieldModel()->getFieldsetModelAlias();
			$this->set(low(Inflector::pluralize($this->_getFieldModel()->getFieldsetModelAlias())),$this->_getFieldModel()->getFieldsets());
		}
		if (!empty($this->data))
		{
			if ($this->save($this->data)) 
			{
				$this->_afterSave(true);	
			}else
			{
				$this->errorMsg(sprintf(__('The %s could not be saved. Please, try again.', true),$this->displayName));
			}
		}
	}

	protected function _afterSave($created = false)
	{
		$this->successMsg(sprintf(__('The %s has been saved', true),$this->displayName));
		$this->redirect(array('action'=>'index'));
	}
	
	function admin_index()
	{
		checkSessionKey();
		$this->pageTitle = __($this->displayName);
		$fieldsetId = '';
		if($this->_getFieldModel()->useFieldset() == true && isset($this->params['named']['fieldset']) && is_numeric($this->params['named']['fieldset']))
		{
			$fieldsetId = $this->params['named']['fieldset'];
			$fieldsetModelForeignKey = $this->_getFieldModel()->getFieldsetModel()->foreignKey;
			$this->paginate['conditions'] = array($fieldsetModelForeignKey => $fieldsetId);
			$fieldsetName = $this->_getFieldModel()->getFieldsetModel()->field('name',array($this->_getFieldModel()->getFieldsetModel()->primaryKey => $this->params['named']['fieldset']));
			
			$this->pageTitle = sprintf(__("Fields defined in %s fieldset"),$fieldsetName);			
			
			$groups = $this->_getFieldModel()->getFieldProperty($this->_getFieldModel()->getFieldsetModel()->foreignKey,'group');
			$key = array_search("grid",$groups);
			unset($groups[$key]);
			$this->_getFieldModel()->setFieldProperties($this->_getFieldModel()->getFieldsetModel()->foreignKey,array('group'=>$groups));
			
		} 
		
		$this->_getFieldModel()->recursive = 0;
		$this->paginate['limit'] = 10;
		$this->set('fields', $this->paginate());
		$this->set('fieldsetId',$fieldsetId);
	}

	function admin_edit($id = null) {
		$this->pageTitle = __($this->displayName);
		if($id)
		{
			$field =  $this->_getFieldModel()->read(null, $id);
		}
		if (!$id || !$field) {
			$this->errorMsg(sprintf(__('Invalid %s', true),$this->displayName));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$this->data[$this->fieldModelAlias]['id'] = $id;
			if ($this->save($this->data)) {
				$this->_afterSave(true);	
			} else {
				$this->errorMsg(sprintf(__('The %s could not be saved. Please, try again.', true),$this->displayName));
			}
		}
		if (empty($this->data)) {
			$this->data = $field;
			if(isset($this->data[$this->fieldModelAlias]['options']) && is_array($this->data[$this->fieldModelAlias]['options']))
			{
				$this->data[$this->fieldModelAlias]['options'] = implode("\n",$this->data[$this->fieldModelAlias]['options']);
			}
		}
		if($this->_getFieldModel()->useFieldset() == true)
		{
			$this->set(low(Inflector::pluralize($this->_getFieldModel()->getFieldsetModelAlias())),$this->_getFieldModel()->getFieldsets());
		}
		$this->set('field',$field);
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->errorMsg(sprintf(__('Invalid id for %s ', true)),$this->displayName);
			$this->redirect($this->referer());
		}
		if ($this->_getFieldModel()->del($id)) {
			$this->successMsg(sprintf(__('%s deleted', true),$this->displayName));
			$this->redirect($this->referer());
		}
	}
	protected function _getFieldModel()
	{
		return $this->{$this->fieldModelAlias};
	}
	
	protected function save($data)
	{
		$optionsFieldType = $this->_getFieldModel()->getOptionsFieldType();
		$fieldOptionModelAlias = $this->_getFieldModel()->getFieldOptionModelAlias();
		$this->_getFieldModel()->create();
		$returnValue = $this->_getFieldModel()->saveAll($data);
		if($this->_getFieldModel()->useSeparateFieldOption() == true && isset($data[$this->fieldModelAlias]['type']) && in_array($data[$this->fieldModelAlias]['type'], $optionsFieldType))
		{
			$this->_getFieldModel()->_processFieldOptionsAfterSave($this->_getFieldModel()->id);
		}
		return $returnValue;
	}
	
	function beforeRender()
	{
		$this->set(array('displayName'=>$this->displayName,'fieldModelAlias'=>$this->fieldModelAlias));
		$this->set('useFieldset',$this->_getFieldModel()->useFieldset());
		if($this->_getFieldModel()->useFieldset() == true)
		{
			$this->set(array('fieldsetModelAlias'=>$this->_getFieldModel()->getFieldsetModelAlias()));
		}
		parent::beforeRender();
	}
}
?>